package com.acce.gxsc.page.supervision.carInMarket.child;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.bean.TypeBean;
import com.acce.gxsc.model.netBean.InCarBean;
import com.acce.gxsc.page.outCar.detail.OutCarDetailActivity;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.base.mvp.BaseMvpFragment;
import com.acce.sdk.widget.adapterrv.CommonAdapter;
import com.acce.sdk.widget.adapterrv.ViewHolder;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */

public class CarInMarketChildFragment extends BaseMvpFragment<CarInMarketChildView, CarInMarketChildPresenter> implements CarInMarketChildView {

    View                     rootView;
    String                   mQueryType;
    CommonAdapter<InCarBean> mCommonAdapter;
    ArrayList<InCarBean> mData = new ArrayList<>();
    com.acce.sdk.widget.adapterlv.CommonAdapter<TypeBean> mTypeAdapter;
    ArrayList<TypeBean> mTypeBean       = new ArrayList<>();
    int                 lastVisibleItem = 0;

    @Bind(R.id.tv_count)
    TextView           mTvCount;
    @Bind(R.id.spn_type)
    Spinner            mSpnType;
    @Bind(R.id.rv)
    RecyclerView       mRv;
    @Bind(R.id.srl)
    SwipeRefreshLayout mSrl;

    public static CarInMarketChildFragment newInstance(String queryType) {
        Bundle args = new Bundle();
        args.putString("queryType", queryType);
        CarInMarketChildFragment fragment = new CarInMarketChildFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected CarInMarketChildPresenter createPresenter() {
        return new CarInMarketChildPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.car_in_market_child_fragment, container, false);
        ButterKnife.bind(this, rootView);
        init();
        mPresenter.loadCarInMarketList(getQueryTime(), mQueryType, false);
        mPresenter.loadCarInMarketCount(getQueryTime(), mQueryType);
        return rootView;
    }

    private void init() {
        mQueryType = getArguments().getString("queryType");
        mCommonAdapter = new CommonAdapter<InCarBean>(getContext(), R.layout.car_in_market_child_item_car, mData) {
            @Override
            protected void convert(ViewHolder holder, final InCarBean inCarBean, int i) {
                if(mQueryType.equals("10")||mQueryType.equals("20")) {
                    if ("1".equals(inCarBean.superviseOther)) {
                        holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.color_00ff1e));
                    } else if ("10".equals(inCarBean.inCarRemarks)) {
                        holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.white));
                    } else {
                        holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.red));
                    }
                }else{
                    if ("1".equals(inCarBean.superviseOther)) {
                        holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.color_00ff1e));
                    }else {
                        holder.setBackgroundColor(R.id.rl_content, Tools.getColor(R.color.white));
                    }
                }
                holder.setText(R.id.tv_plate_num, Tools.getString3(inCarBean.plateNum, inCarBean.containNo, inCarBean.hangNum));
                holder.setText(R.id.tv_contact_name, Tools.getString4(inCarBean.contactName, inCarBean.underCompany));
                holder.setText(R.id.tv_create_time, Tools.getTime(inCarBean.createTime));
                holder.setOnClickListener(R.id.rl_content, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent outCarDetailIntent = new Intent(CarInMarketChildFragment.this.getContext(), OutCarDetailActivity.class);
                        outCarDetailIntent.putExtra("queryId", inCarBean.inCarId);
                        outCarDetailIntent.putExtra("isOutCar", false);
                        startActivity(outCarDetailIntent);
                    }
                });
            }
        };
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRv.setLayoutManager(linearLayoutManager);
        mRv.setAdapter(mCommonAdapter);
        mSrl.setSize(SwipeRefreshLayout.DEFAULT);
        mSrl.setColorSchemeResources(R.color.colorPrimary, R.color.material_blue, R.color.material_green);
        mSrl.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadCarInMarketList(getQueryTime(), mQueryType, false);
                mPresenter.loadCarInMarketCount(getQueryTime(), mQueryType);
            }
        });
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                if (!mSrl.isRefreshing() && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mCommonAdapter.getItemCount()) {
                    loadMore();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }
        });
        switch (mQueryType) {
            case "10":
                mTypeBean.add(new TypeBean("10", "当日1号门进场车辆"));
                mTypeBean.add(new TypeBean("20", "72小时内1号门进场车辆"));
                break;
            case "20":
                mTypeBean.add(new TypeBean("10", "当日1号门进场车辆"));
                mTypeBean.add(new TypeBean("20", "72小时内1号门进场车辆"));
                break;
            case "30":
                mTypeBean.add(new TypeBean("", "区域内所有车辆"));
                mTypeBean.add(new TypeBean("整柜-私人柜", "整柜-私人柜"));
                mTypeBean.add(new TypeBean("整柜-月结公司", "整柜-月结公司"));
                mTypeBean.add(new TypeBean("整柜-国冷、通泰、卓志", "整柜-国冷、通泰、卓志"));
                mTypeBean.add(new TypeBean("散货-国冷、通泰、卓志", "散货-国冷、通泰、卓志"));
                mTypeBean.add(new TypeBean("散货-空运货", "散货-空运货"));
                mTypeBean.add(new TypeBean("散货-精品货", "散货-精品货"));
                mTypeBean.add(new TypeBean("整/散-国产货", "整/散-国产货"));
                mTypeBean.add(new TypeBean("登记-拼货车辆", "登记-拼货车辆"));
                mTypeBean.add(new TypeBean("登记-违规补单", "登记-违规补单"));
                mTypeBean.add(new TypeBean("登记-场地费", "登记-场地费"));
                mTypeBean.add(new TypeBean("登记-机动位", "登记-机动位"));
                mTypeBean.add(new TypeBean("登记-采购车辆", "登记-采购车辆"));
                mTypeBean.add(new TypeBean("登记-往来业务车辆", "登记-往来业务车辆"));
                mTypeBean.add(new TypeBean("错单修改", "错单修改"));
                break;
            case "40":
                mTypeBean.add(new TypeBean("40", "5小时之内新区口登记车辆"));
                mTypeBean.add(new TypeBean("10", "当天进场车辆"));
                mTypeBean.add(new TypeBean("登记-违规补单", "登记-违规补单"));
                break;
            case "50":
                mTypeBean.add(new TypeBean("40", "5小时之内农园棚登记车辆"));
                mTypeBean.add(new TypeBean("10", "当天进场车辆"));
                mTypeBean.add(new TypeBean("登记-违规补单", "登记-违规补单"));
                break;
            case "60":
                mTypeBean.add(new TypeBean("40", "5小时之内其他区域登记车辆"));
                mTypeBean.add(new TypeBean("10", "当天进场车辆"));
                mTypeBean.add(new TypeBean("登记-违规补单", "登记-违规补单"));
                break;
        }
        mSpnType.setAdapter(mTypeAdapter = new com.acce.sdk.widget.adapterlv.CommonAdapter<TypeBean>(getContext(), R.layout.input_spinner_item_2, mTypeBean) {
            @Override
            protected void convert(com.acce.sdk.widget.adapterlv.ViewHolder viewHolder, TypeBean typeBean, int i) {
                viewHolder.setText(R.id.tv_content, typeBean.name);
            }
        });
        mSpnType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                mPresenter.loadCarInMarketList(getQueryTime(), mQueryType, false);
                mPresenter.loadCarInMarketCount(getQueryTime(), mQueryType);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void loadMore() {
        mPresenter.loadCarInMarketList(getQueryTime(), mQueryType, true);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showProgressDialog(String s, boolean b) {
        ((BaseActivity) getActivity()).showProgressDialog(s, b);
    }

    @Override
    public void hideProgressDialog() {
        ((BaseActivity) getActivity()).hideProgressDialog();
    }

    Runnable openRunnable;
    Runnable closeRunnable;

    public void setRefreshing(boolean refreshing) {
        if (refreshing) {
            if (openRunnable == null) {
                openRunnable = new Runnable() {
                    @Override
                    public void run() {
                        if (mSrl != null) {
                            mSrl.setRefreshing(true);
                        }
                    }
                };
            }
            getActivity().getWindow().getDecorView().post(openRunnable);
        } else {
            if (closeRunnable == null) {
                closeRunnable = new Runnable() {
                    @Override
                    public void run() {
                        if (mSrl != null) {
                            mSrl.setRefreshing(false);
                        }
                    }
                };
            }
            getActivity().getWindow().getDecorView().postDelayed(closeRunnable, 300);
        }
    }

    @Override
    public void loadCarInMarketListSuccess(boolean isLoadMore, ArrayList<InCarBean> inCarList) {
        if (!isLoadMore) {
            mCommonAdapter.getDatas().clear();
        }
        if (inCarList != null) {
            mCommonAdapter.getDatas().addAll(inCarList);
        }
        mCommonAdapter.notifyDataSetChanged();
    }

    @Override
    public void loadCarInMarketCount(String s){
        mTvCount.setText(Tools.getString(s));
    }

    public String getQueryTime(){
        return mTypeBean.get(mSpnType.getSelectedItemPosition()).no;
    }
}
